package com.qihang.core.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.qihang.common.PagerModel;
import com.qihang.core.dao.MenuDao;
import com.qihang.core.domain.MenuEntity;
import com.qihang.core.domain.UserEntity;

/**
 * @author limn
 *
 */
@Repository("menuDao")
public class MenuDaoImpl implements MenuDao {

	@Resource(name="sessionFactory")
	protected SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean isExistMenu(MenuEntity paramMenu) {
		Session session = this.getSession();
		String hql = "select count(1) from MenuEntity where menuName =?";
		Query query = session.createQuery(hql);
		query.setString(0, paramMenu.getMenuName());
		Long count = (Long) query.uniqueResult();
		if (count >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public void delMenu(MenuEntity paramMenu) {
		this.getSession().delete(paramMenu);
	}

	@Override
	public void updateMenu(MenuEntity paramMenu) {
		this.getSession().delete(paramMenu);
	}

	@Override
	public PagerModel getMenuList(MenuEntity paramMenu) {
		String hql = "from MenuEntity";
		Query query = this.getSession().createQuery(hql);
		PagerModel pages = new PagerModel(paramMenu.getPagerStart(), 3, query);
		return pages;
	}

	@Override
	public MenuEntity getMenu(MenuEntity paramMenu) {
		return (MenuEntity) this.getSession().get(MenuEntity.class, paramMenu.getMenuId());
	}

	@Override
	public boolean addMenu(MenuEntity paramMenu) {
		Serializable succ = this.getSession().save(paramMenu);
		return null == succ ? false : true;
	}
	
	
	
}
